package com.gitee.wsl.time

import kotlinx.datetime.LocalDateTime

enum class WeekendPolicy {
    THURSDAY_FRIDAY,
    FRIDAY_SATURDAY,
    SATURDAY_SUNDAY;

    companion object {
        fun daysToWeekend(policy: WeekendPolicy, date: LocalDateTime): Int {
            val dow = date.dayOfWeek.ordinal
            when (policy) {
                THURSDAY_FRIDAY -> {
                    if (dow < 4) {
                        return 4 - dow
                    }
                    if (dow > 5) {
                        return 7 - dow + 4
                    }
                }

                FRIDAY_SATURDAY -> {
                    if (dow < 5) {
                        return 6 - dow
                    }
                    if (dow > 6) {
                        return 7 - dow + 5
                    }
                }

                SATURDAY_SUNDAY -> if (dow < 6) {
                    return ( 6 - dow).coerceAtLeast(0)
                }

            }
            return 0
        }

        fun daysFromWeekend(policy: WeekendPolicy, date: LocalDateTime): Int {
            val dow = date.dayOfWeek.ordinal
            when (policy) {
                THURSDAY_FRIDAY -> {
                    if (dow < 4) {
                        return dow
                    }
                    if (dow > 5) {
                        return dow - 5
                    }
                }
                FRIDAY_SATURDAY -> {
                    if (dow < 5) {
                        return dow
                    }
                    if (dow > 6) {
                        return dow - 6
                    }
                }

                SATURDAY_SUNDAY -> if (dow < 6) {
                    return dow
                }
            }
            return 0
        }
    }
}

